<?php

class Silk_Pms_Block_Exchange_Jifen_Abstract extends Mage_Core_Block_Template
{
	private $_exchange = null;
	private $_today;

	public function getExchange()
	{
	    if($this->_exchange) return $this->_exchange;

	    $this->_exchange = Mage::getModel('pms/exchange')->load($this->getRequest()->getParam('exchange_id'));

	    if(!$this->_exchange || !$this->_exchange->getId())
	    {
	        throw new Exception("活动不存在！");
	    }
	    return $this->_exchange;
	}
	
	public function isshow(){
		date_default_timezone_set('PRC'); 
		$model = $this->getExchange();
		$endat = $model->getEndAt();
		$endday = strtotime($endat);
		$this->_today = strtotime(date('Y-m-d H:i:s'));
		if($this->_today>$endday){
			return false;
		}else{
			$status = $model->getStatus();
			if($status){
				return true;
			}else{
				return false;
			}	
		}

	}
	public function getRate(){
		$customer = Mage::getSingleton('customer/session');
		Mage::getSingleton('customer/session')
            ->setCustomerId($customer->getId())
            ->setCustomerGroupId($customer->getGroupId());
    	$rule = Mage::helper('rewardpoints/calculation_spending')->getSpendingRateAsRule();
        $rate = $rule->getPointsSpended() / $rule->getBaseRate();
        return $rate;
	}
	public function getRateApp(){
		$exchange = $this->getExchange();
		$websiteId = $exchange->getWebsiteId();
        $collection = Mage::getModel('rewardpoints/rate')->getSpendingRates()->addFieldToFilter("status",1);
        //add by wanglin start 增加功能，单个站点设置积分比例。
		/*$collection = Mage::getModel('rewardpoints/rate')->getSpendingRates()
            ->addFieldToFilter("website_ids",$websiteId)
            ->addFieldToFilter("status",1);*/
        //add by wanglin end

		if($collection->count()){
			$item = $collection->getFirstItem();
			$websites = $item->getWebsiteIds();
			$websites =  explode(",",$websites);
			if(in_array($websiteId, $websites)){
				$rate = $item->getPoints() / $item->getMoney();
				return $rate;
			}else{
				return null;
			}
		}else{
			return null;
		}

	}
 	public function getProduct($productId,$websiteId){
		$website = Mage::getModel('core/website')->load($websiteId);
		$storeId = $website->getDefaultStore()->getId();
		$collection = Mage::getModel('catalog/product')->getCollection()
					->addAttributeToSelect('*')
		            ->addAttributeToFilter('entity_id', array('in'=>$productId));
		$collection->joinAttribute(
            'status',
            'catalog_product/status',
            'entity_id',
            null,
            'inner',
            $storeId
        );
		$collection->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));
 		 $isShowOutOfStock = Mage::getStoreConfigFlag('cataloginventory/options/show_out_of_stock');
        if(!$isShowOutOfStock)
        {
            Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
        }
        return $collection->getFirstItem();


 	}

	public function hasProduct($productId,$websiteId){
		$website = Mage::getModel('core/website')->load($websiteId);
		$storeId = $website->getDefaultStore()->getId();
		$collection = Mage::getModel('catalog/product')->getCollection()
					->addAttributeToSelect('*')
		            ->addAttributeToFilter('entity_id', array('in'=>$productId));
		$collection->joinAttribute(
            'status',
            'catalog_product/status',
            'entity_id',
            null,
            'inner',
            $storeId
        );
		$collection->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));

		 $isShowOutOfStock = Mage::getStoreConfigFlag('cataloginventory/options/show_out_of_stock');
		if(!$isShowOutOfStock)
		{
			Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
		}
		$productIds = array();
		foreach ($collection as $product) {
		$productIds[]= $product->getId();
		}
		$diff = array_diff($productIds, $productId);
		if(count($diff)){
			return $productIds;
		}else{
			return $productId;
		}

	}
	public function getExchangeProduct($productId,$id){
		$exchangeproducts = Mage::getModel("pms/exchangeproduct")
			                  ->getCollection()
			                  ->addFieldToFilter("product_id",$productId)
			                  ->addFieldToFilter("activity_id",$id);
		$item = $exchangeproducts->getFirstItem();
		return $item;
	}
}